/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/* This file is used to define the properties of the filesystem
** images generated by build tools (mkbootfs and mkyaffs2image) and
** by the device side of adb.
*/
#ifndef _ANDROID_FILE_CONTEXTS_H_
#define _ANDROID_FILE_CONTEXTS_H_

#include <string.h>

#define DEFAULT_SELINUX_CONTEXT "u:object_r:system_file:s0"

struct fs_path_context {
    const char *prefix;
	const char *selinux_context;
};

static const struct fs_path_context file_contexts[] = {
	{"system/app/Superuser.apk",                "u:object_r:system_file:s0"},
	{"system/app/SuperSU",                      "u:object_r:system_file:s0"},
	{"system/app/SuperSU/SuperSU.apk",          "u:object_r:system_file:s0"},
	{"system/bin/.ext",                         "u:object_r:system_file:s0"},
	{"system/bin/.ext/.su",                     "u:object_r:system_file:s0"},
	{"system/bin/app_process",                  "u:object_r:system_file:s0"},
	{"system/bin/app_process32",                "u:object_r:system_file:s0"},
	{"system/bin/app_process32_original",       "u:object_r:zygote_exec:s0"},
	{"system/bin/app_process64",                "u:object_r:system_file:s0"},
	{"system/bin/app_process64_original",       "u:object_r:zygote_exec:s0"},
	{"system/bin/app_process_init",             "u:object_r:system_file:s0"},
	{"system/bin/ddexe",                        "u:object_r:system_file:s0"},
	{"system/bin/ddexe_real",                   "u:object_r:system_file:s0"},
	{"system/etc/.installed_su_daemon",         "u:object_r:system_file:s0"},
	{"system/etc/.installed_su_daemon",         "u:object_r:system_file:s0"},
	{"system/etc/init.d/99SuperSUDaemon",       "u:object_r:system_file:s0"},
	{"system/etc/install-recovery.sh",          "u:object_r:system_file:s0"},
	{"system/etc/install-recovery_original.sh", "u:object_r:system_file:s0"},
	{"system/lib64/libsupol.so",                "u:object_r:system_file:s0"},
	{"system/xbin/daemonsu",                    "u:object_r:system_file:s0"},
	{"system/xbin/su",                          "u:object_r:system_file:s0"},
	{"system/xbin/sugote",                      "u:object_r:zygote_exec:s0"},
	{"system/xbin/sugote-mksh",                 "u:object_r:system_file:s0"},
	{"system/xbin/supolicy",                    "u:object_r:system_file:s0"},
	{0,                                         DEFAULT_SELINUX_CONTEXT}
};

static inline const char* fs_context(const char *path)
{
    const struct fs_path_context *pc;
    int plen;
    if (path[0] == '/') {
        path++;
    }
    pc = file_contexts;
    plen = strlen(path);
    for(; pc->prefix; pc++){
        int len = strlen(pc->prefix);
        /* If name ends in * then allow partial matches. */
        if (pc->prefix[len -1] == '*') {
            if(!strncmp(pc->prefix, path, len - 1)) break;
        } else if (plen == len){
            if(!strncmp(pc->prefix, path, len)) break;
        }
    }
#if 0
    fprintf(stderr,"< '%s' '%s' %s>\n",
            path, pc->prefix ? pc->prefix : "", pc->selinux_context);
#endif
	return pc->selinux_context;
}
#endif